
package ma.ensias.healthme.dao;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import ma.ensias.healthme.entities.Symptome;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;


public class SymptomeDao {

    HttpClient client = new DefaultHttpClient();;
    HttpPost post = new HttpPost(DaoUtils.URL+"symptome.php");;

    public SymptomeDao() {
//        try {
//            HttpClient client = new DefaultHttpClient();
//            HttpPost post = new HttpPost(DaoUtils.URL+"symptome.php");
//
//        } catch (Exception e) {
//            System.out.println("error");
//        }
    }

    public void insert(Symptome symptome) {
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(DaoUtils.URL+"symptome.php");
        try {
            List<NameValuePair> donnes = new ArrayList<NameValuePair>(1);

            //assign data
            donnes.add(new BasicNameValuePair("requet", "insert"));
            donnes.add(new BasicNameValuePair("id", String.valueOf(symptome.getId())));
            donnes.add(new BasicNameValuePair("nom", symptome.getNom()));
            donnes.add(new BasicNameValuePair("description", symptome.getDescription()));

            post.setEntity(new UrlEncodedFormEntity(donnes));
            client.execute(post);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        } catch (Exception e) {
            System.out.println("error");
        }
    }
    
    public void update(Symptome symptome) {
        try {
            List<NameValuePair> donnes = new ArrayList<NameValuePair>(1);

            //assign data
            donnes.add(new BasicNameValuePair("requet", "update"));
            donnes.add(new BasicNameValuePair("id", String.valueOf(symptome.getId())));
            donnes.add(new BasicNameValuePair("nom", symptome.getNom()));
            donnes.add(new BasicNameValuePair("description", symptome.getDescription()));
            

            post.setEntity(new UrlEncodedFormEntity(donnes));
            client.execute(post);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void delete(Symptome symptome){
        this.delete(symptome.getId());
    }

    private void delete(int id) {
        try {
            List<NameValuePair> donnes = new ArrayList<NameValuePair>(1);

            //assign data
            donnes.add(new BasicNameValuePair("requet", "delete"));
            donnes.add(new BasicNameValuePair("id", String.valueOf(id)));
            

            post.setEntity(new UrlEncodedFormEntity(donnes));
            client.execute(post);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    
     public  List<Symptome> getSymptomes(){
        List maladies = new LinkedList<Symptome>();
        StringBuffer sb = new StringBuffer("");
        BufferedReader br = null;
        
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet();
            URI uri = new URI(DaoUtils.URL+"selectSymptome.php");
            get.setURI(uri);
            HttpResponse reponse = client.execute(get);
            InputStream is;
            is = reponse.getEntity().getContent();
            br = new BufferedReader(new InputStreamReader(is));
            String ligneLue = br.readLine();
            while (ligneLue != null) {
                sb.append(ligneLue);
                sb.append("\n");
                ligneLue = br.readLine();
            }
        } catch (Exception e) {
        } // faut fermer le buffer utilisé
        finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("error");
                }
            }
        }
        
        try {
            JSONArray jArray = new JSONArray(sb.toString());
            int    id;
            String nom = new String();
            String desc = new String();
            for (int i = 0; i < jArray.length(); i++) {
	           id =  Integer.valueOf(jArray.getJSONObject(i).getString("id").toString()).intValue();
	            nom = jArray.getJSONObject(i).getString("nom").toString();
	            desc = jArray.getJSONObject(i).getString("description").toString();
	           maladies.add(new Symptome(id, nom, desc));
            }
        } catch (JSONException je) {
        }
        return maladies;
        
        
    }
    
    public Symptome getSymptome(int id){
        List<Symptome> maladies = new LinkedList<Symptome>();
        maladies = this.getSymptomes();
        for(Symptome m : maladies){
            if(m.getId() == id)
                return m;
        }
        return null;
    }
    
}
